﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ScrumLite.Data.Tests
{
    /// <summary>
    /// Summary description for RoleStoreFixture
    /// </summary>
    [TestClass]
    public class AbilityStoreFixture
    {
        IAbilityStore store;

        static Guid a1Id = new Guid("1F4F9968-8D5E-4d8a-9BD8-9A8DF1434B5D");
        static Guid a2Id = new Guid("1F4F9968-8D5E-4d8a-9BD8-9A8DF1434B5F");
        static Guid a3Id = new Guid("1F4F9968-8D5E-4d8a-9BD8-9A8DF1434B60");

        [TestInitialize]
        public void Initialize()
        {
            store = new AbilityStore();
        }

        [TestMethod()]
        public void ShouldRetrieveAbilities()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                CreateFakeAbilityStore();

                Ability[] actual = this.store.GetAbilities();
                Assert.AreEqual(3, actual.Length);

                Assert.IsNotNull(actual.FirstOrDefault(a => a.id == a1Id));
                Assert.IsNotNull(actual.FirstOrDefault(a => a.id == a2Id));
                Assert.IsNotNull(actual.FirstOrDefault(a => a.id == a3Id));
            }
        }

        private static void CreateFakeAbilityStore()
        {

            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                Ability ability1 = new Ability { id = a1Id, name = "Ability 1" };
                Ability ability2 = new Ability { id = a2Id, name = "Ability 2" };
                Ability ability3 = new Ability { id = a3Id, name = "Ability 3" };

                context.RoleAbilities.DeleteAllOnSubmit(context.RoleAbilities);
                context.Abilities.DeleteAllOnSubmit(context.Abilities);

                context.Abilities.InsertOnSubmit(ability1);
                context.Abilities.InsertOnSubmit(ability2);
                context.Abilities.InsertOnSubmit(ability3);

                context.SubmitChanges();
            }
        }
    }
}
